﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Helper for both Ai and PlayerController; 
/// takes an angle from them and uses that to determine which sprite from an array should be rendered.
/// </summary>
public class FrameFromDirection : MonoBehaviour {

    public List<Sprite> Sprites;

    [HideInInspector]
    public SpriteRenderer SR;
    [HideInInspector]
    public int FrameI = 0;
    public bool Inverse = false;

    void Awake() {
        SR = GetComponent<SpriteRenderer>();
    }


    public void set(float ang) {
        
        ang += 180.0f;
        if(Inverse) ang = 720.0f - ang;

        ang *= (float)Sprites.Count / 360.0f;
        ang += Sprites.Count - 0.5f;
        FrameI = Mathf.CeilToInt(ang) % Sprites.Count;
        SR.sprite = Sprites[FrameI];

    }
}
